//
//  BespeakTableViewController.swift
//  DriverSchoolAPP
//
//  Created by Logel on 2017/5/11.
//  Copyright © 2017年 Logel. All rights reserved.
//

import UIKit

class BespeakTableViewController: UITableViewController,BespeakTableViewCellDelegate,LgCallBack{
    var pageNo = 1
    var rcList = [Reservations]()
    let header = MJRefreshNormalHeader()
    let footer = MJRefreshAutoNormalFooter()
    override func viewDidLoad() {
        super.viewDidLoad()
        let conformBtn = UIBarButtonItem(title: "注意", style: .plain, target: self, action: nil)
        //conformBtn.tintColor = UIColor.black
        self.navigationItem.rightBarButtonItem = conformBtn
        header.setRefreshingTarget(self, refreshingAction:#selector(self.headerRefresh))
        self.tableView.mj_header = header
        footer.setRefreshingTarget(self, refreshingAction:#selector(self.footerRefresh))
        self.tableView.mj_footer = footer
        getData()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return rcList.count
    }

    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "reuseCell", for: indexPath) as! BespeakTableViewCell
        cell.selectNum = indexPath.row
        cell.delegate = self
        cell.rs = rcList[indexPath.row]
        // Configure the cell...

        return cell
    }
    
    func headerRefresh(){
        print("下拉刷新.")
        sleep(1)
        rcList.removeAll()
        //重现生成数据
        pageNo = 1
        getData()
        
    }
    
    func footerRefresh(){
        print("提拉加载")
        sleep(1)
        pageNo += 1
        getData()
    }

    
    func getData(){
        asyn_req_post(method: M_Reservations, cb: self, p:
        ["accountId":user.accountId as AnyObject,"pageNo":pageNo as AnyObject,"pageSize":10 as AnyObject])
    }
    
    func bespeakBtnClick(indexPath: Int) {
        print(indexPath)
        asyn_req_post(method: M_CancelReser, cb: self, p: ["reserId":rcList[indexPath].id as AnyObject])
    }

    func callBack(method: String, data: AnyObject) {
        let j = JSON(data)
        print(j)
        
        let head = parseMobileHead(x: j["MobileHead"])
        if head.code == "1" {
            
            switch method {
            case M_Reservations :
                rcList += parseReservationsList(all: j["MobileBody"].arrayValue)
                tableView.reloadData()
            default:
                showMsg(msg: "预约成功")
                getData()
                header.endRefreshing()
                footer.endRefreshing()
            }
            
            
        }else{
            showMsg(msg: head.message)
        }
    }
}
